#!/usr/bin/perl

use Modern::Perl;
use lib './lib';

use File::Find;
use File::Slurp;
use Log::Log4perl qw( :easy );
use Try::Tiny;

require AEF::AEFSearch;

Log::Log4perl->easy_init($INFO);

my @directories_to_search = @ARGV;
if(!@directories_to_search)
{
	print "Usage $0 directory\n";
	print "Search directories specified for AEF's and display their version numbers.\n";
	exit;
}
my $search = AEF::AEFSearch->new(IgnoreNonPEFiles => 1);
my $logger = get_logger();

find(\&wanted, @directories_to_search);

sub wanted { 
	/\.AEF$/i && -f &&
	CheckFile($_);
}

sub CheckFile
{
	my $file = shift;
	my $logger = get_logger();
	$logger->debug("Looking for version in $file");
	my $data = read_file($file, binmode => ':raw');
	try
	{
		my $version = $search->GetVersion($data);
		if($version)
		{
			$logger->info($File::Find::name . " - $version");
		}
	} catch {
		$logger->warn("Failed to process $File::Find::name - $_");
	}
}

